'use strict';

const Controller = require('egg').Controller;

/**
* @controller CardController 注释必写，swagger-doc是根据这段注释来生成接口的 ）。
*/
class CardController extends Controller {
    async list(){
         /**
        * @summary card列表分页。
        * @description card列表分页。
        * @router get /cardlist
        * @request query integer limit
        * @request query integer page
        * @request query String OltId
        */
        const params = this.ctx.query
        const result = await this.ctx.service.card.list(params)
        this.ctx.body = {
            msg:"",
            status:200,
            count:result.count,
            data:result.rows
        }
    }
    async delete(){
         /**
        * @summary card删除。
        * @description card删除。
        * @router get /carddelete
        * @request query String id
        */
        const params = this.ctx.query
        let cardResult = await this.ctx.model.Card.findOne({_id:params.id})
        const result = await this.ctx.service.card.delete(params)
        let oltResult = await this.ctx.model.Olt.findOne({_id:cardResult.oltId})
        global.log.desc = `${oltResult.name} ${oltResult.oltIP} card ${cardResult.slot} `+result.msg
        this.ctx.body = {
            data:result,
            status:200,
        }
    }
    async create(){
          /**
        * @summary card创建。
        * @description card创建。
        * @router get /cardcreate
        * @request query String id
        */
        const params = this.ctx.query
        const result = await this.ctx.service.card.create(params)
        let cardResult = await this.ctx.model.Card.findOne({_id:params.id})
        let oltResult = await this.ctx.model.Olt.findOne({_id:cardResult.oltId})
        global.log.desc = `${oltResult.name} ${oltResult.oltIP} card ${cardResult.slot} `+result.msg
        this.ctx.body = {
            data:result,
            status:200,
        }
    }
}

module.exports = CardController;
